// rxr
const initialState = { allChannels: [] as channelsType[], userChannels: [] as channelsType[], currentId: 0 as number };
export default function channelsReducer(state = initialState, action: any) {
  switch (action.type) {
    case 'channels/setChannels':
      return {
        ...state,
        allChannels: action.payload,
      };
    case 'channels/setUserChannels':
      return {
        ...state,
        userChannels: action.payload,
      };
    case 'channels/addUserChannels':
      const newItem = state.allChannels.find((item) => {
        return item.id === action.payload;
      });
      return {
        ...state,
        userChannels: [...state.userChannels, newItem as channelsType].sort((a: any, b: any) => {
          return a.id - b.id;
        }),
      };
    case 'channels/delUserChannels':
      return {
        ...state,
        userChannels: state.userChannels.filter((item) => {
          return item.id !== action.payload;
        }),
      };
    case 'channels/setCurrentId':
      return {
        ...state,
        currentId: action.payload,
      };
    default:
      return state;
  }
}
export interface channelsType {
  id: number;
  name: string;
}
